package test.data_structure.queue;

import test.data_structure.array.MyArray;

import java.util.Random;

public class MyLoopQueue<T> implements Queue<T> {

    private T[] data;

    private int front, tail;

    private int size;

    public MyLoopQueue(int capacity) {
        this.data = (T[])new Object[capacity + 1];
        this.front = 0;
        this.tail = 0;
        this.size = 0;
    }

    public MyLoopQueue() {
        this(10);
    }

    public int getCapacity() {
        return data.length - 1;
    }

    @Override
    public boolean isEmpty() {
        return this.front == this.tail;
    }

    public boolean isFull() {
        return (this.tail + 1) % this.data.length == this.front;
    }

    @Override
    public int getSize() {
        return this.size;
    }

    @Override
    public void enqueue(T data) {
        if (isFull()) {
            resize(getCapacity() * 2);
        }
        this.data[tail] = data;
        this.tail = (this.tail + 1) % this.data.length;
        this.size ++;
    }

    private void resize(int newCapacity) {
        T[] newData = (T[])new Object[newCapacity];
        for (int i = 0; i < size ; i ++) {
            newData[i] = data[(i + front) % data.length];
        }
        data = newData;
        front = 0;
        tail = size;
    }

    @Override
    public T dequeue() {
        if (isEmpty()) {
            throw new IllegalArgumentException("cannot dequeue from an empty queue.");
        }
        T d = this.data[front];
        this.data[front] = null;
        front = (front + 1) % this.data.length;
        size --;
        if (size == getCapacity() / 4 && getCapacity() / 2 != 0) {
            resize(getCapacity() / 2);
        }
        return d;

    }

    @Override
    public T getFront() {
        if (isEmpty()) {
            throw new IllegalArgumentException("queue is empty");
        }
        return data[front];
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(String.format("Loop queue Size: %d, Capacity: %d\n", getSize(), getCapacity()));
        sb.append("front [");
        for (int i = front ; i != tail ; i = (i + 1) % data.length ) {
            sb.append(data[i]);
            if ((i + 1) % data.length != tail) {
                sb.append(", ");
            }
        }
        sb.append("] tail");
        return sb.toString();
    }

    public static void main(String[] args) {
        MyArrayQueue<Integer> arrayQueue = new MyArrayQueue<>();
        MyLoopQueue<Integer> loopQueue = new MyLoopQueue<>();
        System.out.println("array queue cost time(s):" + testQueue(arrayQueue, 100000));
        System.out.println("loop queue cost time(s):" + testQueue(loopQueue, 100000));
//        for (int i = 0 ; i < 10 ; i ++) {
//            queue.enqueue(i);
//            System.out.println(queue);
//            if (i % 3 == 2) {
//                queue.dequeue();
//                System.out.println(queue);
//            }
//        }
    }

    public static double testQueue(Queue<Integer> q, int opCount) {
        long start = System.nanoTime();
        Random random = new Random();
        for (int i = 0 ; i < opCount ; i++) {
            q.enqueue(random.nextInt(Integer.MAX_VALUE));
        }
        for (int i = 0 ; i < opCount ; i++) {
            q.dequeue();
        }
        long end = System.nanoTime();
        return (end - start) / 1000000000.0;
    }

}
